// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { game } from "../game/control/game";
import { rankItemType } from "../game/gameType";
import rankItem from "../prefab/rank/rankItem";
import { ListViewAdapter } from "../util/ListView/ListViewAdapter";
import { ccs } from "../util/services";
import { WinBase } from "./WinBase";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends WinBase {

    @property(ListViewAdapter)
    list: ListViewAdapter = null;

    rankList: rankItemType[] = null;

    @property(rankItem)
    myRank: rankItem = null;
    
    myRankData = null;

    upList: rankItemType[] = null;
    myInfo = null;
    start () {
        ccs.netMgr._init();

        this.getMyInfo();
        ccs.winMgr.showWin('loading');
        this.scheduleOnce(() => {
            this.getRank();
        },1)
        //绑定点击
        this.touchEndEventScale(this.nodes.rankUp, () => {
            this.upRank();
        })
        this.touchEndEventScale(this.nodes.rankMoney, () => {
            this.sortList("money");
            ccs.winMgr.showWin('toast', { msg: "财富排行"});
            this.list.updateItems(this.upList);
        })
        this.touchEndEventScale(this.nodes.rankChild, () => {
            this.sortList("child");
            ccs.winMgr.showWin('toast', { msg: "生娃排行"});
            this.list.updateItems(this.upList);
        })
        this.touchEndEventScale(this.nodes.rankIncome, () => {
            this.sortList("monthIncome");
            ccs.winMgr.showWin('toast', { msg: "工资排行"});
            this.list.updateItems(this.upList);
        })

    }
    sortList(sortId: string) {
        this.upList.sort((a: rankItemType, b: rankItemType) => {
            return  b[sortId] - a[sortId];
        })
    }
    getRank() {

        ccs.netMgr._callCloud("rank", {
            action: "getRank",
            params: {}
        },
        (res) => {
            if(res.data && res.data.length > 0) {
                this.rankList = res.data.filter(val => val.money && val.money > 0);
                this.upList = this.rankList.slice(0, 20);
                this.list.updateItems(this.upList);
                this.myRankData = this.rankList.findIndex(val => val.id == game.playerMgr.heroInfo.id && val.money > 0);
                this.setMyRank();
            }
        })
    }
    setMyRank() {
        this.myRank.initItem(this.myInfo);
        this.myRank.rankIndex.string = this.myRankData > -1? this.myRankData + 1: "未上榜";
    }
    getMyInfo() {
        let worker = game.playerMgr;
        let info = worker.heroInfo;
        let work = worker.getWork();
        this.myInfo = {                                                                                                                                                                                                                                                 
            id: info.id,
            name: info.name,
            age: info.age,
            work: work? work.levelName[work.level]: "无业",
            money: info.money,
            monthIncome: work? work.income: 0,
            child: worker.bag.figureBag.bag.items.reduce((val, cur) => {
                if(cur.relation == 'son') {
                    return val += 1;
                }
                return 0;
            },0)
        }
    }
    upRank() {
        ccs.netMgr._callCloud("rank", {
            action: "upRank",
            params: this.myInfo,
        },(res) => {
            ccs.winMgr.showWin('toast', { msg: "成功参与富豪榜，快看看你上榜了没"});
            this.getRank();
        })
    }
    onCell() {
        
    }
    // update (dt) {}
}
